﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;

namespace CPU_test
{
    public partial class Mainform : Form
    {
        
        volatile uint execs = 0;
        Thread[] threads;
        object locker = new object();
 
        public Mainform()
        {
            threads = new Thread[Environment.ProcessorCount];
            InitializeComponent();

            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);

        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                threads[i].Abort();
            }
            Environment.Exit(0);
            

        }

        bool running = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (!running)
            {
                timer1.Start();
                running = true;
                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    threads[i] = new Thread(new ThreadStart(loop1));
                    threads[i].Start();
                }
                button1.Text = "Stop";
            }
            else
            {
                timer1.Stop();
                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    threads[i].Abort();
                }
                execs = 0;
                running = false;
                button1.Text = "Start";
            }

                
         

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string better_execs = (execs * Environment.ProcessorCount).ToString();
            label1.Text = "Crude indication of Loops/sec: " + (execs*Environment.ProcessorCount).ToString() + " (That's " + better_execs.Length.ToString() + " digits.)";

                

        }
        public void loop1()
        {
            while (true)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                for (int i = 0; i < 10000000; i++) ;
                stopwatch.Stop();
                execs = (1000 / (uint)stopwatch.ElapsedMilliseconds) * 10000000;
            }
        }

       


        private void Form1_Load_1(object sender, EventArgs e)
        {
            RegistryKey RegKey = Registry.LocalMachine;
            RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
            Object cpuSpeed = RegKey.GetValue("~MHz");
            Object cpuType = RegKey.GetValue("VendorIdentifier");
            label2.Text = "Amount of threads: " + Environment.ProcessorCount.ToString() + ". Cpu speed: " + cpuSpeed.ToString() + "mhz. Cpu type: " + cpuType.ToString();
        }

    }
}
